package com.qg.cyapp.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qg.cyapp.R;
import com.qg.cyapp.bean.FoodBean;
import com.qg.cyapp.bean.FoodSeries;
import com.qg.cyapp.component.MenuWaterFall;
import com.qg.cyapp.component.MenuWaterFall.ScrollToBottomListener;
import com.qg.cyapp.component.WaterFall;
import com.qg.cyapp.util.ShareUtil;

public class NormalDishesActivity extends HttpActivity implements
		ScrollToBottomListener {

	// 菜系
	private List<String> caixiString = new ArrayList<String>();
	private List<FoodSeries> caixi = new ArrayList<FoodSeries>();
	private Spinner spCaiXi;
	private ArrayAdapter<String> caixiAdapter = null; // 省级适配器

	private PopupWindow popupwindow;
	public static List<FoodBean> foods = new ArrayList<FoodBean>();
	private LinearLayout llFoods;
	private TextView tvSearch, tvSelect;
	private EditText etSearch;
	private MenuWaterFall wfFoods;
	private int startId = 0;
	private int number = 6;
	private boolean canLoad = false;

	private int caixiPosition = 0; // =0为全部菜系

	private final int GET_FOOD = 0;
	private final int GET_CAIXI = 1;
	private final int GET_CAIXI_FOOD = 2;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub

		bean = getGsonBean(result);
		if (bean.state == ShareUtil.SUCCESS) {
			switch (requestCode) {
			case GET_FOOD:
			case GET_CAIXI_FOOD:
				List<FoodBean> newFoods = gson.fromJson(bean.json,
						new TypeToken<List<FoodBean>>() {
						}.getType());
				startId += newFoods.size();
				foods.addAll(newFoods);
				wfFoods.notifyDataSetChanged();
				break;
			case GET_CAIXI:

				// 其他菜系
				List<FoodSeries> newSeries = gson.fromJson(bean.json,
						new TypeToken<List<FoodSeries>>() {
						}.getType());
				caixi.addAll(newSeries);
				for (FoodSeries fs : caixi) {
					caixiString.add(fs.name);
				}
				caixiAdapter.notifyDataSetChanged();
				break;

			}
		}
		canLoad = true;

	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_normal_dishes;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		spCaiXi = (Spinner) findViewById(R.id.spin_caixi);
		spCaiXi.setSelection(0, true);
		caixiAdapter = new ArrayAdapter<String>(NormalDishesActivity.this,
				R.layout.item_adapter_caixi, caixiString);
		caixiAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCaiXi.setAdapter(caixiAdapter);
		spCaiXi.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				spCaiXi.setSelection(position, true);
				if (caixiPosition != position) {
					caixiPosition = position;
					foods.clear();
					startId = 0;
					wfFoods.removeAllViews();
					getFood();
				}
				parent.setVisibility(View.VISIBLE);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		llFoods = (LinearLayout) findViewById(R.id.llFoods);
		etSearch = (EditText) findViewById(R.id.et_menu_search);
		tvSearch = (TextView) mfindViewAndSetOnClick(R.id.tv_menu_searchBt);

		wfFoods = new MenuWaterFall(this, foods, ShareUtil.NormalDishesActivity);
		wfFoods.init(3);
		wfFoods.setScrollBottomListener(this);
		llFoods.addView(wfFoods);

		getNormalFood();

		// 第一个为全部菜系
		FoodSeries foodSeries = new FoodSeries();
		foodSeries.id = null;
		foodSeries.name = "全部菜系";
		caixi.add(foodSeries);
		caixiAdapter.notifyDataSetChanged();
		getCaixi();
	}

	public void getFood() {
		if (caixiPosition == 0) {
			getNormalFood();
		} else {
			getCaixiFood(caixi.get(caixiPosition).id);
		}
	}

	public void getNormalFood() {
		canLoad = false;
		params.clear();
		params.put("menudirno", ShareUtil.NORMAL_FOOD);
		params.put("startId", String.valueOf(startId));
		params.put("number", String.valueOf(number));
		params.put("action", "getFood");
		addPostRequest("GetFoodAction", params, GET_FOOD);
		sendPostRequest();
	}

	public void getCaixiFood(String id) {
		canLoad = false;
		params.clear();
		params.put("menudirno", ShareUtil.NORMAL_FOOD);
		params.put("startId", String.valueOf(startId));
		params.put("number", String.valueOf(number));
		params.put("cuisineno", id);
		params.put("action", "getCuisineFood");
		addPostRequest("GetFoodAction", params, GET_CAIXI_FOOD);
		sendPostRequest();
	}

	public void getCaixi() {
		canLoad = false;
		params.clear();
		params.put("menudirno", ShareUtil.NORMAL_FOOD);
		params.put("action", "getCuisine");
		addPostRequest("GetFoodAction", params, GET_CAIXI);
		sendPostRequest();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.tv_menu_searchBt:
			Intent intent = new Intent(NormalDishesActivity.this,
					SearchShowActivity.class);
			String searchWord = etSearch.getText().toString();
			intent.putExtra(ShareUtil.SEARCH_KEY, searchWord);
			startActivity(intent);
			break;
		}
	}

	@Override
	public void scrollToBottom() {
		// TODO Auto-generated method stub
		Log.v(TAG, "scrollToBottom");
		if (canLoad) {
			getFood();
			canLoad = false;
		}
	}

}
